package com.example.retry;

import java.util.concurrent.Executor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@SpringBootApplication
@EnableAsync
@EnableRetry
@EnableScheduling
@EnableAspectJAutoProxy
public class SpringRetryApplication {

	private static final Logger LOGGER = LoggerFactory.getLogger(SpringRetryApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(SpringRetryApplication.class, args);
	}

	@Bean("asyncExecutor")
	public Executor asyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(4);
		executor.setMaxPoolSize(8);
		executor.setKeepAliveSeconds(60);
		executor.setQueueCapacity(1024);
		executor.setThreadNamePrefix("async-");
		executor.setRejectedExecutionHandler((r, e) -> {
			LOGGER.warn("线程池[{}]任务队列已满,尝试由主线程执行!", "async-");
			if (!e.isShutdown()) {
				r.run();
			}
		});
		return executor;
	}

}
